<?php
class SetPageStatus extends Action {
	private $status = array ('draft','approving','approved','unapproved','published' );
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		$I = check_login ( ADMIN_LOGIN_PAGE );
		$ids = rqst ( 'pid' );
		$ids = safe_ids ( $ids, ",", true );
		if (empty ( $ids )) {
			sess_add ( 'page_tip_info', '错误的网页编号' );
			Response::redirect ( $this->referer );
		}
		$pageM = new WebPageModel ();
		$rst = false;
		if (rqset ( 'del' )) {
			$del = irqst ( 'del' );
			$deleted = $del ? 1 : 0;
			$rst = $pageM->update ( array ('deleted' => $deleted,'update_uid' => $I->uid ), array ('page_id IN' => $ids ) );
			$s = 'deleted';
		} else {
			$s = rqst ( 's', 'draft' );
			$s = in_array ( $s, $this->status ) ? $s : 'draft';
			$data = array ('status' => $s,'update_uid' => $I->uid,'publish_time' => 0 );
			if ($s == 'published') {
				$ids = apply_filter ( 'on_publish_page', $ids, null ); // 此时可以去掉一些未满足发布条件的网页
				$data ['publish_time'] = time ();
				if (! icando ( 'publish', 'page' )) { // 如果我不能直接发布
					$where ['status'] = 'approved';
				}
			}
			if ($ids !== false) {
				$where = array ('page_id IN' => $ids );
				$rst = $pageM->update ( $data, $where );
			}
		}
		if ($rst === false) {
			sess_add ( 'page_tip_info', '出错啦:' . $pageM->last_error_msg () );
		}
		Response::redirect ( './?status=' . $s );
	}
}